(function(){
	let iconspan = document.querySelector("body>div:first-child>span:first-child");
	iconspan.addEventListener("click", ()=>history.go(-1));
	let xhr = new XMLHttpRequest();
	xhr.open("GET","../serverMovie.php?hide=2");
	xhr.send(null);
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			let phoneArr = JSON.parse(xhr.responseText);
			myform[0].oninput = function(){
				let count = 0;
				phoneArr.forEach(val=>{val[0] == this.value && (count = 1);})
				if(count == 0){
					myform[0].maxLength = myform[0].value.indexOf("+86") != -1 ? "14" : "11";
					this.style.color = phoneReg.test(this.value) ?  "green" : "red";
					myform[0].nextElementSibling.innerText = "获取验证码";
					myform[0].nextElementSibling.style.color = "white";
				}else{
					myform[0].nextElementSibling.innerText = "该号已注册";
					myform[0].nextElementSibling.style.color = "red";
				}
			}
		}
	}

	let myform = document.forms[0];
	let phoneReg = /^(\+86)?1[3-9][0-9]{9}$/;
	
	myform[0].nextElementSibling.onclick = function(){
		if(phoneReg.test(myform[0].value)){
			var arr = [];
			for (var i = 48; i <= 122; i++) {
				(i<=57 || i>=65 && i<=90 || i>=97) && (arr[arr.length] = String.fromCharCode(i));
			}
			for (var i = 1,arr2 = []; i < 5; i++) {
				arr2.push(arr[parseInt(Math.random()*arr.length)]);
			}
			this.innerText = arr2.join("");
		}
	}
	myform[1].oninput = function(){
		if(this.value.toUpperCase() == myform[0].nextElementSibling.innerText.toUpperCase() && this.value != "获取验证码"  && this.value != "该号已注册"){
			this.style.color = "green";
		}else{
			this.style.color = "red";
		}
	}
	myform.onsubmit = function(){
			return false;
	}
	myform.elements[2].onclick = function(){
		var n = 0
		for(let i = 0; i < myform.elements.length - 1; i++){
			myform.elements[i].style.color == "green" && n++;
		}
		if(n < myform.elements.length - 1){
			console.log(myform.elements[1].style.color);
			return false;
		}else{
			sessionStorage.phoneNumber = myform[0].value;
			open("../html/registerAfter.html","_self");
		}
	}
	

})()